package com.tj.bo.trade.server.service.rule;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.tj.bo.trade.client.enums.OrderStatusEnum;
import com.tj.bo.trade.client.enums.TradeTypeEnum;
import com.tj.bo.trade.server.entity.BinaryTradeEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 当天用户净利润率超过10%,收益率调整为比大盘低14%
 * Author: Amarendar
 * Date: 2020/8/25 上午11:01
 */
@Service
public class TradeSpecialCaseRule extends BaseTradeRule {

    private static final String RULE_NAME = "当天用户净利润率超过10%,收益率调整为比大盘低14%";

    private static final Integer DEFAULT_PRIORITY = 1;

    @Override
    public void checkRouterRule(RouterContext routerContext, List<RuleResultVO> ruleResultVOList) {
        Date startDate = DateUtil.getPatternDate(new Date(), "yyyy-MM-dd");
        Date endDate = DateUtil.addDate(startDate, 1);
        List<BinaryTradeEntity> binaryTradeEntityList = binaryTradeDao.selectsBinaryTradeListByTime(routerContext.getUserCode(), routerContext.getProductCode(), OrderStatusEnum.CLOSING.getCode(), TradeTypeEnum.REAL_TRADE.getCode(), startDate, endDate);
        if (CollectionUtils.isEmpty(binaryTradeEntityList) || binaryTradeEntityList.size() < redisConfigUtil.getChangeOddsUserTradeNumbers()) {
            ruleResultVOList.add(new RuleResultVO(routerContext.getProductCode(), RULE_NAME).setMessage("订单数过少"));
            return;
        }
        double todayTotalAmount = 0;
        double todayTotalProfit = 0;

        for (BinaryTradeEntity binaryTradeEntity : binaryTradeEntityList) {
            if (binaryTradeEntity.getAmount().compareTo(binaryTradeEntity.getProfitAmount()) == 0) {
                continue;
            }
            todayTotalAmount += binaryTradeEntity.getAmount().doubleValue();
            todayTotalProfit += (binaryTradeEntity.getProfitAmount().doubleValue() - binaryTradeEntity.getAmount().doubleValue());
        }

        if (todayTotalProfit > 0 && todayTotalAmount > 0 && todayTotalProfit / todayTotalAmount >= 0.1) {
            RuleResultVO ruleResultVO = new RuleResultVO(routerContext.getProductCode(), RULE_NAME);
            ruleResultVO.setPriority(DEFAULT_PRIORITY);
            ruleResultVO.setMode(0);
            ruleResultVO.setChangeValue(BigDecimal.valueOf(-0.14));
            ruleResultVO.setMessage("当天用户净利润率超过10%,收益率调整为比大盘低14%");
            ruleResultVOList.add(ruleResultVO);
        } else {
            ruleResultVOList.add(new RuleResultVO(routerContext.getProductCode(), RULE_NAME).setMessage("当天用户净利润率超过10%,收益率调整为比大盘低14%-条件不满足"));
        }
    }
}
